package com.klhappy.houyuan.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.klhappy.houyuan.utils.imageselector.PictureDialog;
import com.klhappy.houyuan.utils.library.TagCloudLayout;
import com.klhappy.houyuan.utils.library.TagDrawableAdapter;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.ImageAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.GoodsAllBean;
import com.klhappy.houyuan.bean.OnSelfGoodBean.Comment;
import com.klhappy.houyuan.bean.OnSelfGoodBean.Contents;
import com.klhappy.houyuan.bean.OnSelfGoodBean.GoodsImages;
import com.klhappy.houyuan.bean.OnSelfGoodBean.GoodsProtys;
import com.klhappy.houyuan.bean.Images;
import com.klhappy.houyuan.bean.OnSelfGoodBean.OneSelfGoodBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.ShoppingDialog;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.HouYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.view.CircleImageView;
import com.klhappy.houyuan.view.ImageManager;
import com.klhappy.houyuan.view.NotifyingScrollView;
import com.klhappy.houyuan.view.banner.ADInfo;
import com.klhappy.houyuan.view.banner.BannerViewPicasso;
import com.klhappy.houyuan.view.banner.CircleFlowIndicatorPicasso;

//鲜菜 详情页
public class XiangqingActivity extends AbsActivity implements CallBackInterModle {
    @InjectView(R.id.good_name)
    TextView good_name;
    @InjectView(R.id.tv_unit)
    TextView tv_unit;
    @InjectView(R.id.tv_saleVolume)
    TextView tv_saleVolume;
    @InjectView(R.id.tv_titleRatio)
    TextView tv_titleRatio;
    @InjectView(R.id.tv_titleCount)
    TextView tv_titleCount;
    @InjectView(R.id.tl_desTag)
    TagCloudLayout tlDesTag;
    @InjectView(R.id.tv_price)
    TextView tvPrice;
    @InjectView(R.id.tv_originalPrice)
    TextView tv_originalPrice;
    @InjectView(R.id.preferential)
    TextView preferential;
    @InjectView(R.id.web_v)
    WebView web_v;
    @InjectView(R.id.aplh_linear)
    LinearLayout aplh_linear;
    @InjectView(R.id.scr1)
    NotifyingScrollView scr1;
    @InjectView(R.id.banner_view)
    BannerViewPicasso bannerView;
    @InjectView(R.id.view_indecator)
    CircleFlowIndicatorPicasso viewIndicator;
    @InjectView(R.id.iv_icon)
    CircleImageView iv_icon;
    @InjectView(R.id.tv_UserName)
    TextView tv_UserName;
    @InjectView(R.id.tv_Content)
    TextView tv_Content;
    @InjectView(R.id.tv_time)
    TextView tv_time;
    @InjectView(R.id.gv_img)
    GridView gv_img;

    @OnClick(R.id.back_li)
    public void onclick() {
        finish();
    }

    @OnClick(R.id.rating)
    public void rating() {
        ShoppingDialog dialog = new ShoppingDialog(XiangqingActivity.this, goodBean);
        dialog.show();
    }

    @OnClick(R.id.confirmorder)
    public void confirmorder() {
        Intent intent = new Intent(this, ConfirmOrderActivity.class);
        intent.putExtra("goods", itemList);
        this.startActivity(intent);
    }

    @OnClick(R.id.tv_moreEvalation)
    public void tv_moreEvalation() {
        Intent intent = new Intent(this, MoreEvaluationActivity.class);
        intent.putExtra("g_id", goodBean.getG_id() + "");
        startActivity(intent);
    }

    private String goodId;//传递商品ID
    private HouYuanModle modle;
    private OneSelfGoodBean goodBean;
    private List<GoodsImages> goodsImages;
    private Comment comment;
    private List<GoodsProtys> goodsProtyses;
    private Contents contents;
    private List<Images> images;
    private ArrayList<ADInfo> infos = new ArrayList<ADInfo>();
    private ArrayList<GoodsAllBean> itemList;
    private ImageAdapter adapter;
    private PictureDialog dialog;
    private List<String> img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goodsdetail);
        ButterKnife.inject(this);
        goodId = (String) getIntent().getSerializableExtra("goodId");
        titleAlph();

        dialog = new PictureDialog();
        modle = new HouYuanModle(this);
        modle.setCallBackInterModle(this);
        modle.getGoodDetail(goodId);
        showDialog("请稍等...");
        img = new ArrayList<String>();
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.GOODS_DETAIL_URL)) {
            if (o.getCode() == 200) {
                try {
                    goodBean = JsonCode.getClass(o.getEntity(), OneSelfGoodBean.class);
                    goodsProtyses = JsonCode.getClassList(goodBean.getGoods_protys(), GoodsProtys.class);
                    goodsImages = JsonCode.getClassList(goodBean.getGoodsImages(), GoodsImages.class);
                    comment = JsonCode.getClass(goodBean.getComment(), Comment.class);
                    contents = JsonCode.getClass(comment.getContents(), Contents.class);
                    images = JsonCode.getClassList(contents.getImages(), Images.class);
                    if (goodBean != null) {
                        itemList = new ArrayList<>();
                        itemList.add(new GoodsAllBean(goodBean.getG_id() ,goodBean.getG_icon()
                                , goodBean.getG_name(), goodBean.getG_price()
                                , goodBean.getG_unit(), "1" ,""));
                        intView();
                    }
                } catch (Exception e) {
                    Log.e("this", "looger:" + e);
                }
            }
            closeDialog();
        }
    }

    private void intView() {
        good_name.setText(goodBean.getG_name());
        tvPrice.setText(goodBean.getG_price());
        preferential.setText(goodBean.getActiveTags());
        tv_unit.setText(goodBean.getG_unit());
        if (goodBean.getG_price() == goodBean.getPre_price()
                || "".equals(goodBean.getPre_price())
                || null == goodBean.getPre_price()){
            tv_originalPrice.setVisibility(View.INVISIBLE);
        }else {
            addStrikeSpan("￥" + goodBean.getPre_price());
        }
        tv_saleVolume.setText("月销量" + goodBean.getSaleNo() + "件");
        tv_titleRatio.setText("商品好评（好评率" + comment.getPercent() + "）");
        tv_titleCount.setText(comment.getReplyNo() + "已评价");
        web_v.loadData(goodBean.getG_detail(), "text/html", "UTF-8");
        tlDesTag.setAdapter(new TagDrawableAdapter(this, goodsProtyses));
        tv_UserName.setText(contents.getOwn_nickname());
        tv_Content.setText(contents.getCo_cont());
        tv_time.setText(contents.getCo_date());
        ImageManager.loadImage(UrlConstant.BASE_URL + contents.getOwn_icon(), iv_icon);
        adapter = new ImageAdapter(this, images);
        gv_img.setAdapter(adapter);
        gv_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                img.clear();
                for (int i = 0; i < images.size(); i++){
                    img.add(UrlConstant.BASE_URL + images.get(i).getImagePath());
                }
                dialog.setCurItem(position);
                dialog.setOnView(XiangqingActivity.this, img);
                dialog.show();
            }
        });
        if (!"".equals(goodsImages) && null != goodsImages) {
            for (int i = 0; i < goodsImages.size(); i++) {
                ADInfo info = new ADInfo();
                info.setUrl(UrlConstant.BASE_URL + goodsImages.get(i).getImagePath());
                infos.add(info);
            }
            bannerView.setBannerImageViewUrl(this, infos);
            bannerView.setFlowIndicator(viewIndicator);
            bannerView.setTimeSpan(2000);
            bannerView.startAutoFlowTimer();
        }
    }

    /**
     * 设置动态显示标题栏
     */
    private void titleAlph() {
        aplh_linear.getBackground().setAlpha(0);
        scr1.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                int lHeight = 400;
                if (t <= lHeight) {
                    int progress = (int) (new Float(t) / new Float(lHeight) * 255);
                    aplh_linear.getBackground().setAlpha(progress);
                } else {
                    aplh_linear.getBackground().setAlpha(255);
                }
            }
        });
    }

    /**
     * 删除线
     */
    private void addStrikeSpan(String price) {
        SpannableString spanString = new SpannableString(price);
        StrikethroughSpan span = new StrikethroughSpan();
        spanString.setSpan(span, 0, price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_originalPrice.append(spanString);
    }
}
